package yxy.game.pm2.active.command.celebration;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;

/**
 * 国庆寻宝下一层请求
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayLotteryNext
 * @Author jingtao-zheng
 * @Date 2022/9/21 20:57
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayLotteryNext, classtype = NationalDayMessage.NationalDayLotteryNextRequest.class)
public class CMD_Active_国庆寻宝_NationalDayLotteryNext extends Command<NationalDayMessage.NationalDayLotteryNextRequest> {

    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayLotteryNextRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayLotteryNextResponse.Builder response = NationalDayMessage.NationalDayLotteryNextResponse.newBuilder();
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
            if (am.tryLock()) {
                try {
                    //1.当前层数
                    int currFloor = am.getFloor();
                    //2.重置寻宝
                    am.resetCell();
                    am.set();
                    response.setFloor(currFloor);
                    send(messageSource, response, Result.Success);
                } catch (Exception e) {
                    e.printStackTrace();
                    send(messageSource, response, Result.DataError);
                } finally {
                    am.unlock();
                }
            } else {
                send(messageSource, response, Result.DataError);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayLotteryNextResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
